package com.nowcoder.community.controller;

import com.nowcoder.community.entity.DiscussPost;
import com.nowcoder.community.entity.Page;
import com.nowcoder.community.entity.User;
import com.nowcoder.community.service.DiscussPostService;
import com.nowcoder.community.service.LikeService;
import com.nowcoder.community.service.UserService;
import com.nowcoder.community.util.CommunityConstant;
import com.nowcoder.community.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@CrossOrigin
public class HomeController implements CommunityConstant {

    @Autowired
    private UserService userService;

    @Autowired
    private DiscussPostService discussPostService;

    @Autowired
    private LikeService likeService;

    @Autowired
    private HostHolder hostHolder;

    /**
     * 返回论坛首页
     *
     * @param model
     * @param page
     * @return
     */
    @RequestMapping(path = "/index", method = RequestMethod.GET)
    public String getIndexPage(Model model, Page page, @RequestParam(name = "orderMode", defaultValue = "0") int orderMode) {
        User user = hostHolder.getUsers();
        // 方法调用前, SpringMVC会自动实例化Model和Page, 并将page对象注入model(通过DispatchServlet完成)
        page.setRows(discussPostService.findDiscussPostRows(0));
        page.setPath("/index?orderMode=" + orderMode);
        List<DiscussPost> list = discussPostService.findDiscussPosts(0, page.getOffSet(), page.getLimit(), orderMode);
        List<HashMap<String, Object>> discussPosts =  new ArrayList<>();
        if(list != null) {
            for(DiscussPost o : list) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("post", o);
                map.put("user", userService.findUserById(o.getUserId()));
                // 查询帖子点赞数量
                map.put("likeCount", likeService.findEntityLikeCount(ENTITY_TYPE_POST, o.getId()));
                // 查询当前用户对该条帖子的赞状态
                // 如果有用户登陆的话
                if(user != null) {
                    map.put("likeStatus", likeService.findEntityLikeStatus(user.getId(), ENTITY_TYPE_POST, o.getId()));
                }
                else {
                    map.put("likeStatus", 0L);
                }
                discussPosts.add(map);
            }
        }
        model.addAttribute("discussPosts", discussPosts);
        model.addAttribute("orderMode", orderMode);
        return "/index";
    }

    @RequestMapping(path = "/error", method = RequestMethod.GET)
    public String getErrorPage() {
        return "/error/500";
    }

    @RequestMapping(path = "/denied", method = RequestMethod.GET)
    public String getDeniedPage() {
        return "/error/404";
    }

}
